package com.squid.templateProject.bean;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.squid.templateProject.bean.enums.SystemCodeMsgEnums;
import com.squid.templateProject.bean.enums.error.BaseErrorCodeMsg;
import com.squid.templateProject.utils.TimeUtils;
import lombok.Getter;
import lombok.Setter;

/**
 * 常见回报
 *
 * @author jh
 * @date 2023/05/10
 */
@Setter
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Return<T> {
    /**
     * 代码
     */
    private Integer code;
    /**
     * 消息
     */
    private String message;
    /**
     * 成功
     */
    private Boolean success;

    /**
     * 时间戳
     */
    private String timestamp;
    /**
     * 请求
     */
    private String request;
    /**
     * 数据
     */
    private T data;

    private Return() {

    }

    private Return(String timestamp) {
        this.timestamp = timestamp;
    }

    /**
     * 成功
     *
     * @return {@link Return }<{@link T }>
     * @author jh
     * @date 2023/05/10
     */
    public static <T> Return<T> success() {
        return new Return<T>(TimeUtils.getCurrentTime())
                .addSuccess(SystemCodeMsgEnums.OK.getSuccess())
                .addCode(SystemCodeMsgEnums.OK.getCode())
                .addMessage(SystemCodeMsgEnums.OK.getMessage());
    }

    /**
     * 通用返回失败
     *
     * @return {@link Return }<{@link T }>
     * @author jh
     * @date 2023/05/10
     */
    public static <T> Return<T> failure() {
        return new Return<T>(TimeUtils.getCurrentTime())
                .addSuccess(SystemCodeMsgEnums.ERROR.getSuccess())
                .addCode(SystemCodeMsgEnums.ERROR.getCode())
                .addMessage(SystemCodeMsgEnums.ERROR.getMessage());

    }

    /**
     * 设置结果，形参为结果枚举
     *
     * @param result 结果
     * @return {@link Return }<{@link T }>
     * @author jh
     * @date 2023/05/10
     */
    public static <T> Return<T> addResult(BaseErrorCodeMsg result) {
        return new Return<T>(TimeUtils.getCurrentTime())
                .addSuccess(result.getSuccess())
                .addCode(result.getCode())
                .addMessage(result.getMessage());

    }

    private Return<T> addSuccess(Boolean success) {
        this.success = success;
        return this;
    }

    private Return<T> addCode(Integer code) {
        this.code = code;
        return this;
    }

    private Return<T> addMessage(String message) {
        this.message = message;
        return this;
    }

    private Return<T> addTimestamp(String timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    private Return<T> addData(T data) {
        this.data = data;
        return this;
    }

    private Return<T> addRequest(String request) {
        this.request = request;
        return this;
    }

    /**
     * 数据
     *
     * @param object 对象
     * @return {@link Return }<{@link T }>
     * @author jh
     * @date 2023/05/10
     */
    public Return<T> data(T object) {
        return this.addData(object);

    }


    /**
     * 消息
     *
     * @param message 消息
     * @return {@link Return }<{@link T }>
     * @author jh
     * @date 2023/05/10
     */// 自定义状态信息
    public Return<T> message(String message) {
        return this.addMessage(message);

    }

    /**
     * 代码
     *
     * @param code 代码
     * @return {@link Return }<{@link T }>
     * @author jh
     * @date 2023/05/10
     */
    public Return<T> code(Integer code) {
        return this.addCode(code);

    }


    /**
     * 成功
     *
     * @param success 成功
     * @return {@link Return }<{@link T }>
     * @author jh
     * @date 2023/05/10
     */
    public Return<T> success(Boolean success) {
        return this.addSuccess(success);

    }

    /**
     * 成功
     *
     * @param request 请求
     * @return {@link Return }<{@link T }>
     * @author jh
     * @date 2023/05/12
     */
    public Return<T> request(String request) {
        return this.addRequest(request);

    }

}